﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.Business;
using SD.DataAccess;
using Microsoft.VisualBasic;
namespace SD.Web.Iteration1.Contacts
{
    public partial class EditContact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    string tempId = Request.QueryString["id"].ToString();

                    if (!Information.IsNumeric(tempId)) // query string is not numeric
                    {
                        Response.Redirect("ListContact.aspx"); // back to list contact page
                    }
                    int contactId = Convert.ToInt32(tempId);
                    Contact contact = ContactBUS.GetContactById(contactId); // get contact by contact id
                    if (contact == null) // contact is not found
                    {
                        Response.Redirect("ListContact.aspx"); // back to list contact page   
                    }
                    // Found contact
                    // so we bind data to form.

                    // load data for drop down list best method type
                    BindDataForBestMethod(contact);

                    // load data for drop down list contact type
                    BindDataForContactType(contact);

                    

                    // load data for textbox
                    txtFirstName.Text = contact.FirstName;
                    txtSurname.Text = contact.Surname;
                    txtKnowAs.Text = contact.KnownAs;
                    txtOfficePhone.Text = contact.OfficePhone;
                    txtMobilePhone.Text = contact.MobilePhone;
                    txtHomePhone.Text = contact.HomePhone;
                    txtEmailAddress.Text = contact.Email;
                    
                    // get manager name for textbox ManagerName
                    if (contact.ManageId == null || contact.ManageId.Value == 0) // current contact has no manageId
                    {
                        txtManagerName.Text = "";
                        ContactLookup.ContactId = 0;
                    }
                    else // has manageId
                    {
                        var manager = ContactBUS.GetContactById(contact.ManageId.Value);
                        txtManagerName.Text = manager.FirstName + " " + manager.Surname;
                        ContactLookup.ContactId = manager.ContactId;
                    }
                    txtRole.Text = contact.JobRole;

                    txtJobTitle.Text = contact.JobTitle;
                    txtWorkBase.Text = contact.WorkBase;
                    CheckBoxIsActive.Checked = contact.IsActive;
                }
                else // query string id is null
                {
                    Response.Redirect("ListContact.aspx"); // so we go back to list contact page
                }
            }
        }
        
        /// <summary>
        /// Bind data  for drop down list best method contact
        /// </summary>
        /// <param name="contact">contact</param>
        private void BindDataForBestMethod(Contact contact)
        {
            ddlBestContactMethod.DataSource = ContactBUS.SelectAllBestMethodContacts();
            ddlBestContactMethod.DataBind();
            ddlBestContactMethod.DataTextField = "BestContactMethodName";
            ddlBestContactMethod.DataValueField = "BestContactMethodId";
            ddlBestContactMethod.Items.Insert(0, new ListItem(""));
            if (contact.BestContactMethodId.HasValue)
            {
                ddlBestContactMethod.SelectedValue = contact.BestContactMethodId.Value.ToString();
            }
            else
            {
                ddlBestContactMethod.SelectedIndex = 0;
            }
            
        }

        /// <summary>
        /// Bind data  for drop down list contact type
        /// </summary>
        /// <param name="contact">contact</param>
        private void BindDataForContactType(Contact contact)
        {
            ddlContactType.DataSource = ContactBUS.SelectAllContactTypes();
            ddlContactType.DataBind();
            ddlContactType.DataTextField = "ContactTypeName";
            ddlContactType.DataValueField = "ContactTypeId";
            ddlContactType.SelectedValue = contact.ContactTypeId.ToString();
            ddlContactType.Items.Insert(0, new ListItem("", "0"));
        }

        protected void LinkButtonLookupClick(object sender, EventArgs e)
        {
            // show contact loopkup
            ContactLookup.Visible = true;
            ContactLookup.Popup();
        }

        protected void btnSaveClick(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["id"] != null) // query string is not null
                {
                    string tempId = Request.QueryString["id"].ToString(); // get query string id value

                    if (!Information.IsNumeric(tempId)) // query string id is not numeric
                    {
                        Response.Redirect("ListContact.aspx"); // so we back to list contact page
                    }
                    int contactId = Convert.ToInt32(tempId);

                    // set value for contact
                    Contact contact = new Contact();
                    contact.ContactId = contactId;
                    contact.FirstName = txtFirstName.Text.Trim();
                    contact.Surname = txtSurname.Text.Trim();
                    contact.KnownAs = txtKnowAs.Text.Trim();
                    contact.OfficePhone = txtOfficePhone.Text.Trim();
                    contact.MobilePhone = txtMobilePhone.Text.Trim();
                    contact.HomePhone = txtHomePhone.Text.Trim();
                    contact.Email = txtEmailAddress.Text.Trim();
                    
                    //// Manage id of current contact is not null
                    //if (string.IsNullOrEmpty(txtManagerName.Text) == false)
                    //{
                    //    if (ContactLookup.ContactId > 0)
                    //    {
                    //        // user choose new manage id from contact loopkup form
                    //        contact.ManageId = ContactLookup.ContactId;
                    //    }
                    //}

                    contact.ManageId = ContactLookup.ContactId;
                    


                    contact.JobRole = txtRole.Text.Trim();
                    contact.JobTitle = txtJobTitle.Text.Trim();
                    contact.WorkBase = txtWorkBase.Text.Trim();
                    contact.IsActive = CheckBoxIsActive.Checked;
                    
                    contact.ContactTypeId = Convert.ToInt32(ddlContactType.SelectedValue);

                    // user don't choos null value from drop down list best contact method
                    if (string.IsNullOrEmpty(ddlBestContactMethod.SelectedValue) == false)
                    {
                        contact.BestContactMethodId = Convert.ToInt32(ddlBestContactMethod.SelectedValue);
                    }
                    // update contact
                    bool result = ContactBUS.UpdateContact(contact);
                    if (result)
                    {
                        Literal1.Text = "Update successfully.";
                    }
                    else
                    {
                        Literal1.Text = "Oop, update failure, please check you input";
                    }
                }
                else // query string id is null
                {
                    Response.Redirect("ListContact.aspx");
                }

            }
            catch (Exception ex)
            {
                Literal1.Text = ex.Message;
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListContact.aspx");
        }
    }
}